package top.zhongyy.mapper;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;
import top.zhongyy.pojo.Blog;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;

import java.util.List;

/**
 * <p>
 * Mapper 接口
 * </p>
 *
 * @author Andy
 * @since 2021-03-25
 */
@Repository
public interface BlogMapper extends BaseMapper<Blog> {
    
    List<Blog> getBlogList();
    
    // 首页显示四篇推荐博客
    @Select("SELECT id,title from t_blog WHERE recommend = 1 AND deleted = 0 ORDER BY create_time DESC LIMIT 0,#{size}")
    List<Blog> recommendBlog(@Param("size") int size);
    
    @Update("update t_blog set views = views + 1 where id = #{id}")
    void updateViews(@Param("id") Long id);
    
    Blog getOneBlog(@Param("blogId") Long blogId);
    
    List<Blog> getAdminBlogList();
    
    List<Blog> indexSearchBlog(@Param("query") String query);
    
    List<Blog> getBlogByType(@Param("typeId") Long typeId);
    
    List<Blog> getBlogByTag(@Param("tagId") String tagId);
    
    // 获取博客所有的年份
    @Select("SELECT DATE_FORMAT(b.create_time, '%Y') AS YEAR FROM t_blog b WHERE deleted = 0 GROUP BY DATE_FORMAT(b.create_time, '%Y') ORDER BY YEAR DESC")
    List<Long> getAllYear();
    
    // 获取该年份下的所有博客
    @Select("SELECT b.id, b.title, b.create_time, b.flag FROM t_blog b WHERE DATE_FORMAT( b.create_time, '%Y' ) = #{year} AND deleted = 0 AND published = 1 ORDER BY b.create_time DESC ")
    List<Blog> getBlogsByYear(Long year);
    
}
